/**
 *
 * @param fileId
 * @return
 */
import store from "../../store/store";
import $ from "jquery";
import {url} from "../../util/config";
import {message} from "antd";
import login from "../login";

let config = require('../../util/config').config

function getNewLogin(fileId,fileType) {
    return getFileContent(fileId,fileType);
}

export default function getFileContent(fileId,fileType){
    let token = localStorage.getItem("token")
    //如果是文章
    if(fileType==config.article){

        //创建一个content对象，该对象会封装返回的的数据
        let content = {
            //附件数组
            urls:[],
            //文章标题
            articleName:null,
            //文章内容
            articleContent:null
        };

        $.ajax({
            async:false,
            headers: {
                'Accept': "application/json",
                'Authorization': token
            },
            url: url.api_url+`FileList/GetFileContent?fileType=article&fileId=${fileId}`,
            type: "GET",
            success: function (result) {
                if(result.result=="Token過期請重新登錄") {
                    login(localStorage.getItem("account"), localStorage.getItem("password"));
                    content = getNewLogin(fileId,fileType)
                }else {
                    console.log("api getFileContent article",result)
                    let parseUrls = [];
                    let parseUrlNames = [];
                    let urls = [];
                    //获取文章内容
                    let articleName = result.result.ARTICLE_NAME
                    let articleContent = result.result.ARTICLE_CONTENT
                    //获取文章的附件url数组
                    // parseUrls = result.result.ATTACH_URL;
                    parseUrls = result.result.ATTACH_URL;
                    console.log("api getFileContent article",parseUrls)
                    sessionStorage.setItem("attachUrls",JSON.stringify(parseUrls))
                    //获取文章的附件名称数组
                    // parseUrlNames = result.result.ATTACH_NAME;
                    parseUrlNames = result.result.ATTACH_NAME;
                    console.log("api getFileContent article",parseUrlNames)
                    sessionStorage.setItem("attachNames",JSON.stringify(parseUrlNames))
                    //for循环之后把相同下标的附件url元素和附件名称元素赋值给urls对应下标的元素
                    for(let i=0;i<parseUrls.length;i++){
                        urls.push(
                            {
                                uid: i,
                                name: parseUrlNames[i],
                                status: 'done',
                                url: parseUrls[i],
                                response: parseUrls[i],
                                thumbUrl: parseUrls[i],
                            }
                        )
                    }
                    //文章内容
                    content.articleName = articleName;
                    content.articleContent = articleContent;
                    //文章的附件数组
                    content.urls = urls;
                }
            },
            error:function (error) {
                message.error("请求异常")
            }
        })

        return content
    }
    //如果是文件
    else if(fileType==config.file){

        let urls = [];
        $.ajax({
            async:false,
            headers: {
                'Accept': "application/json",
                'Authorization': token
            },
            url: url.api_url+`FileList/GetFileContent?fileType=file&fileId=${fileId}`,
            // url: url.api_url+`FileList/DownloadFile?fileName=222`,
            type: 'GET',
            success: function (result) {
                if(result.result=="Token過期請重新登錄") {
                    login(localStorage.getItem("account"), localStorage.getItem("password"));
                    urls = getNewLogin(fileId, fileType)
                }else {
                    let fileUrls = [];
                    let fileUrlNames = [];
                    fileUrls = result.result.FILE_URL;
                    fileUrlNames = result.result.FILE_URL_NAME;
                    ////for循环之后把相同下标的文件url元素和文件名称元素赋值给urls对应下标的元素
                    urls.push(
                        {
                            uid: "1",
                            name: fileUrlNames,
                            status: 'done',
                            url: fileUrls,
                            response: fileUrls,
                            thumbUrl: fileUrls,
                        }
                    )
                }

            },
            error:function (error) {
                message.error("请求异常")
            }
        })
        return urls
    }
}
